﻿using Microsoft.AspNetCore.Components.Web;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.WarehouseManage;
using SmartWMS.Server.BasicsDatumServer.LocationServer.Dto;
using SmartWMS.Server.BasicsDatumServer.UnitServer.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.BasicsDatumServer.UnitServer
{
    public class UnitServers: IUnitServers
    {
        private readonly IRepository<Unit> _unit;

        public UnitServers(IRepository<Unit> unit)
        {
            _unit = unit;
        }

        /// <summary>
        /// 单位列表（查询）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<List<UnitDtoPes>>> GetUnitAsync(UnitPageDto dto)
        {
            try
               {
                var plist = await _unit.GetAllListAsync();
                var list = plist.MapToList<UnitDtoPes>();
                if (!string.IsNullOrEmpty(dto.UnitCode)) { 
                    list=list.Where(x => x.UnitCode.Contains(dto.UnitCode)).ToList();
                }
                if (!string.IsNullOrEmpty(dto.UnitName))
                {
                    list = list.Where(x => x.UnitName.Contains(dto.UnitName)).ToList();
                }
                int totalCount = list.Count();
                int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.size);
                list = list.Skip((dto.page - 1) * dto.size).Take(dto.size).ToList();

                return list != null ? Response<List<UnitDtoPes>>.Ok(list, totalCount, pageCount) : Response<List<UnitDtoPes>>.Fail(new List<UnitDtoPes>());
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 添加单位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddUnitAsync(UnitDtoPes dto)
        {
            try
            {
                var list=  _unit.GetListQuery().FirstOrDefault(t=>t.UnitName==dto.UnitName);
                if (list != null) {
                    return Response<bool>.Fail(false);
                }

                var resault = await _unit.InsertAsync(dto.MapTo<Unit>());
                if (resault > 0)
                {
                    return Response<bool>.Ok(true);
                }
                else
                {
                    return Response<bool>.Fail(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 删除单位数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public  async Task<Response<bool>> DelUnit(UnitDtoPes dto) {
            try
            {
                var list=  _unit.GetListQuery().FirstOrDefault(t=>t.Id==dto.Id);
                var resault = await _unit.DeleteByIdAsync(list);
                return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除方法
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeletesUnit(UnitDtoPes dto) {
            try
            {
                var sid = dto.DelId.Split(',');
                var list = new List<Unit>();
                foreach (var item in sid)
                { 
                    var Plist = _unit.GetListQuery().Where(t => t.Id == int.Parse(item)).ToList();
                    list.AddRange(Plist);
                }
                //批量删除方法
                var resault = await _unit.DeleteAsync(list);
                return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填单位数据方法
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<UnitDtoPes>> ReckUnit(UnitDtoPes dto) {
            try
            {
                var list = _unit.GetListQuery().FirstOrDefault(t => t.Id == dto.Id);
                return list != null ? Response<UnitDtoPes>.Ok(list.MapTo<UnitDtoPes>()) : Response<UnitDtoPes>.Fail(new UnitDtoPes());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改单位数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<bool>> UpdateUnitAsync(UnitDtoPes dto)
        {
            try
            {
                var resault = await _unit.UpdateAsync(dto.MapTo<Unit>());
                return resault > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}
